package com.cloudcc.mobile.view.wel;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import butterknife.Bind;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.EventList.LoginEvent;
import com.cloudcc.cloudframe.bus.EventList.ServerUrlEvent;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.model.UserModel;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.LoginDBModel;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.presenter.LoginPresenter;
import com.cloudcc.mobile.util.AESUtils;
import com.cloudcc.mobile.util.NetWork;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.util.UIUtils;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.login.LoginNewActivity;
import com.cloudcc.mobile.view.main.MainUIActivity;

/**
 * 启动页
 * 
 * @author Administrator
 *         <hr>
 *         登录流程:<br>
 *         1、根据账号名称 去获取app的url 2、根据url去执行登录操作
 *         <hr>
 * 
 */
public class WelcomeActivity extends BaseActivity {
	
	
	@Bind(R.id.progressBar1)
	ProgressBar progressBar;
	private boolean islogining = true;
	private LoginPresenter loginPresenter = new LoginPresenter();
	/**
	 * 是否是第一次运行
	 */
	private boolean isFirst;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				AppContext.isFenXiang = false;// 设为不能分享
				Intent intent = new Intent(mContext, MainUIActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.in_from_right,
						R.anim.out_to_left);// 动画
				WelcomeActivity.this.finish();
				break;
			case 0:
				WelcomeActivity.this.finish();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		if (UserManager.getManager().isFrist()) {
			enterGuide();
		} else {
			initData();
		}
	}
	private void initData() {
		progressBar.setVisibility(View.VISIBLE);
		LoginDBModel login = loginPresenter.getLastLogin();
		if (StringUtils.isBlank(login.userName)
				|| StringUtils.isBlank(login.password)) {
			enterLogin();
			return;
		}
		if (login!=null&&login.isAutoLogin && NetWork.detect(this)) {// 如果是默认选择了自动登录 现在就去登录页面 或者 引导页面
			loginPresenter.getAppUrl(login.userName);
		} else {
			enterLogin();
		}
	}
	/**
	 * 获取应用登录 url
	 * 
	 * @param event
	 */
	public void onEventMainThread(ServerUrlEvent event) {
		if (event.isError()) {
			enterLogin();
			return;
		}
		String url = event.getData().data.url;
		UrlTools.url = url + "/";
		UrlManager.APP_UR = url;
		UrlTools.weixinUrl = url;
		LoginDBModel login = loginPresenter.getLastLogin();
		// 通过jni获取c代码里面保存的密码 然后解密出来
		String dePassword = AESUtils.getDePassword(login.password);
		if (TextUtils.isEmpty(dePassword)) {// 解密失败
			enterLogin();
			return;
		}
		loginPresenter.login(login.userName,dePassword);
	}
	/**
	 * 根据用户名密码 获取binding
	 * 
	 * @param event
	 */
	public void onEventMainThread(LoginEvent event) {
		LogUtils.d(TAG, "登录:" + event.isOk());
		if (event.isError()) {
			enterLogin();
			return;
		}
		RunTimeManager.setAppBinding(event.getData().binding);
		// 保存用户 注意密码
		UserModel user = event.getData().userInfo;
		UserManager.getManager().saveUser(user);
		// 进入到主页面
		enterAPP();

	}

	/**
	 * 进入引导页面
	 * 2016-5-3
	 */
	private void enterGuide() {
		Intent intent = new Intent(WelcomeActivity.this,
				GuideViewActivity.class);
		startActivity(intent);
		this.finish();
	}

	/**
	 *  进入登录页面
	 * 2016-5-3
	 */
	private void enterLogin() {
		Intent intent = new Intent(WelcomeActivity.this, LoginNewActivity.class);
		startActivity(intent);
		this.finish();
	}
	/**
	 * 进入app
	 */
	private void enterAPP(){
		AppContext.isFenXiang =false;
		UIUtils.enterMainUI(mContext);
		finish();
	}
	
	
	@Override
	public int getLayoutId() {
		return R.layout.welcome;
	}
	@Override
	protected boolean hasEventLife() {
		return true;
	}
}
